import string
import time

import web
from demo.tello_python.tello_manager import Tello
# from demo.tello_python.tello import Tello

urls = (
    '/web_tello/left', 'left',
    '/web_tello/right', 'right',
    '/doAction', 'doAction',
    '/doCommand', 'doCommand'
)
app = web.application(urls, globals())


drone = Tello.get_connection("192.168.43.129")
# drone1 = Tello.get_connection("192.168.43.204")

class left:
    def GET(self):
        print('left')
        drone.takeoff()
        drone.left(20)
        return 'left'


class right:
    def GET(self):
        print('right')

        drone.takeoff()
        drone.right(20)
        return 'right'


class doAction:
    def POST(self):
        web.header("Access-Control-Allow-Origin", "*")
        doName = web.input().get("doName")
        drone.takeoff()
        if doName == "up":
            drone.up(20)
        elif doName == "down":
            drone.down(20)
        elif doName == "left":
            drone.left(20)
        elif doName == "right":
            drone.right(20)
        elif doName == "forward":
            drone.forward(20)
        elif doName == "back":
            drone.back(20)
        elif doName == "cw":
            drone.cw(90)
        elif doName == "ccw":
            drone.ccw(90)
        elif doName == "flip":
            drone.flip('l')
        elif doName == "land":
            drone.land()
        return 'ok'


class doCommand:
    def POST(self):
        web.header("Access-Control-Allow-Origin", "*")
        commandString = web.input().get('data')
        commandArr = commandString.split(",")

        # 处理代循环的命令数据
        if commandString == "":
            print("命令为空")
            return "命令为空"

        if commandString.find("for") == -1:
            doCommandArr(commandArr)
        else:
            # 处理命令
            forCommandArr(commandArr)

        return "ok"


def doCommandArr(arr):
    for cm in arr:
        if cm.find("command") != -1:
            cm = "command"
        if cm.find("takeoff") != -1:
            cm = "takeoff"
        if cm.find("land") != -1:
            cm = "land"
        drone.send_command("command", "192.168.43.129")
        drone.send_command(cm, "192.168.43.129")
        drone.send_command("command", "192.168.43.204")
        drone.send_command(cm, "192.168.43.204")
        time.sleep(2)
        print(cm)


def forCommandArr(arr):
    num = 0
    doNum = 1
    dic = {}

    # 处理带循环的命令
    for index in range(len(arr)):
        key = str(num) + "_" + str(doNum)
        if arr[index].find("forStart") >= 0:
            num += 1
            doNum = int(arr[index].replace("forStart ", ""))
            continue
        elif arr[index].find("forEnd") >= 0:
            num += 1
            doNum = 1
            continue
        else:
            if key not in dic.keys():
                dic[key] = []
            dic[key].append(arr[index])

    for strKey in dic.keys():
        num = int(strKey.split("_")[1])
        for i in range(num):
            doCommandArr(dic[strKey])


if __name__ == '__main__':
    app.run()
